package org.jsbd.boss.service.impl;

import java.io.File;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.IHeartbeatDAO;
import org.jsbd.boss.domian.channel.FileUploadVo;
import org.jsbd.boss.domian.channel.Heartbeat;
import org.jsbd.boss.domian.channel.OS;
import org.jsbd.boss.service.IHeartbeatService;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.OssUtils;
import org.jsbd.boss.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import edu.hziee.cap.common.util.Md5Utils;

@Service("heartbeatService")
public class HeartbeatServiceImpl implements IHeartbeatService {
	
	public static final Logger LOGGER = LoggerFactory.getLogger(SystemServiceImpl.class);

	@Resource
	private IHeartbeatDAO heartbeatDao;

	@Override
	public Map<String, Object> getHeartbeatByPage(Heartbeat heartbeat) {
		return heartbeatDao.selectHeartbeatByPage(heartbeat);
	}

	@Override
	@Transactional("transactionManagerCrack")
	public void saveHeartbeat(MultipartFile[] files, Heartbeat heartbeat) {
		if (files != null && files.length > 0) {
			MultipartFile file = files[0];
			FileUploadVo fileVo = uploadFile(file);
			if (fileVo != null) {
				String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("/") + 1);
				String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
				String filePath = fileVo.getFilePath();
				String md5 = fileVo.getMd5();
				if (StringUtils.isNotBlank(fileType)) {
					heartbeat.setMark(md5);
					heartbeat.setFilePath(filePath);
				}
			}
		}
		heartbeatDao.insertHeartbeat(heartbeat);
	}
	
	private FileUploadVo uploadFile(MultipartFile file) {
		String fileUploadPath = PropertiesUtil.getValue("game_file_upload_path");
		FileUploadVo fileVo = null;
		if (file != null && !file.isEmpty()) {
			File apkFile = null;
			try {
				fileVo = new FileUploadVo();
				String originalFilename = file.getOriginalFilename();
				String fileName = originalFilename.substring(originalFilename.lastIndexOf("/") + 1);
				String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
				String fileSaveName = new Date().getTime() + fileName;
				apkFile = new File(fileUploadPath, fileSaveName);
				// 这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉
				FileUtils.copyInputStreamToFile(file.getInputStream(), apkFile);
				String md5 = Md5Utils.md5Hex(FileUtils.readFileToByteArray(apkFile));
				String day = DateUtil.getCurrentDateStr();
				fileName = "heartbeat/" + day + "/" + md5;
				OssUtils.uploadFile(fileName, file.getInputStream());
				fileVo.setMd5(md5);
				fileVo.setFilePath(fileName);
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			} finally {
				if (apkFile != null) {
					apkFile.setWritable(true);
					apkFile.delete();
				}
			}
		}
		return fileVo;
	}

	@Override
	public void updateHeartbeat(String mark, String isDelete) {
		Heartbeat heartbeat = new Heartbeat();
		heartbeat.setMark(mark);
		heartbeat.setIsDelete(isDelete);
		heartbeatDao.updateHeartbeat(heartbeat);
	}

	@Override
	public void phoneTypeEdit(Heartbeat heartbeat) {
		heartbeatDao.phoneTypeEdit(heartbeat);
	}

	@Override
	public Heartbeat getByVersion(String version) {
		return heartbeatDao.selectByVersion(version);
	}
	

}
